﻿
/** 
 * AUTHOR: 你快乐吗　我很快乐
 * TIME: 2024.03.12-20.29.38
 * ATTENTION: onStart 等UI脚本自带函数不可改写为异步执行，有需求的异步逻辑请使用函数封装，通过函数接口在内部使用
 */

import orderControlerUI_Generate from "./ui-generate/orderControlerUI_generate";


export default class orderControlUI extends orderControlerUI_Generate {

	/**当前播放动画 */
	private curTween: Tween<any> = null;
	/**动画时间 (ms)*/
	private animTime: number = 500;

	/**先手位置 */
	private firstPos: Vector2 = null;
	/**后手位置 */
	private secondPos: Vector2 = null;

	protected onStart() {
		//设置能否每帧触发onUpdate
		this.canUpdate = false;
		this.layer = UILayerMiddle;

		// 记录先后手位置
		this.firstPos = this.imageCanvas.position.clone();
		this.secondPos = new Vector2(this.firstPos.x, this.firstPos.y + this.imageCanvas.size.y / 2);
	}

	/**切换为后手 */
	public switchSecond() {

		let starPos = this.firstPos.clone();
		let endPos = this.secondPos.clone();
		this.curTween = new mw.Tween({ pos: starPos.clone() }).to({ pos: endPos }, this.animTime)
			.onUpdate((obj) => {
				this.imageCanvas.position = obj.pos;
				console.log(`后手：${this.imageCanvas.position}`);
			})
			.onComplete(() => {
				this.curTween = null;
			})
			.start();
	}


	/**切换为先手 */
	public switchFirst() {
		let startPos = this.secondPos.clone();
		let endPos = this.firstPos.clone();
		this.curTween = new mw.Tween({ pos: startPos.clone() }).to({ pos: endPos }, this.animTime)
			.onUpdate((obj) => {
				this.imageCanvas.position = obj.pos;
				console.log(`先手：${this.imageCanvas.position}`);
			})
			.onComplete(() => {
				this.curTween = null;
			})
			.start();
	}
}